package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.dto.AnnouncementAddDTO;
import com.ruoyi.merchant.domain.dto.AnnouncementQueryDTO;
import com.ruoyi.merchant.domain.dto.AnnouncementUpdateDTO;
import com.ruoyi.merchant.domain.vo.AnnouncementDetailVO;
import com.ruoyi.merchant.domain.vo.AnnouncementListVO;
import com.ruoyi.merchant.service.IAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家端公告管理Controller
 */
@RestController
@RequestMapping("/merchant/announcement")
public class AnnouncementController extends BaseController {
    
    @Autowired
    private IAnnouncementService announcementService;
    
    /**
     * 新增公告
     */
    @Log(title = "公告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AnnouncementAddDTO addDTO) {
        Long announcementId = announcementService.addAnnouncement(addDTO);
        return AjaxResult.success("新增成功", announcementId);
    }
    
    /**
     * 查询历史公告列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AnnouncementQueryDTO query) {
        startPage();
        List<AnnouncementListVO> list = announcementService.getAnnouncementList(query);
        return getDataTable(list);
    }
    
    /**
     * 查询公告详情
     */
    @GetMapping("/{announcementId}")
    public AjaxResult detail(@PathVariable Long announcementId) {
        AnnouncementDetailVO vo = announcementService.getAnnouncementDetail(announcementId);
        return AjaxResult.success("查询成功", vo);
    }

    /**
     * 更新公告（只能更新草稿）
     */
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{announcementId}")
    public AjaxResult update(@PathVariable Long announcementId,
                             @Validated @RequestBody AnnouncementUpdateDTO updateDTO) {
        boolean result = announcementService.updateAnnouncement(announcementId, updateDTO);
        return result ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
    }

    /**
     * 发布公告（草稿转发布）
     */
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{announcementId}/publish")
    public AjaxResult publish(@PathVariable Long announcementId) {
        boolean result = announcementService.publishAnnouncement(announcementId);
        return result ? AjaxResult.success("发布成功") : AjaxResult.error("发布失败");
    }

    /**
     * 下线公告
     */
    @Log(title = "公告管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{announcementId}/offline")
    public AjaxResult offline(@PathVariable Long announcementId) {
        boolean result = announcementService.offlineAnnouncement(announcementId);
        return result ? AjaxResult.success("下线成功") : AjaxResult.error("下线失败");
    }


    /**
     * 删除公告
     */
    @Log(title = "公告管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{announcementId}")
    public AjaxResult delete(@PathVariable Long announcementId) {
        boolean result = announcementService.deleteAnnouncement(announcementId);
        return result ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
}
